package com.netease.research.controller.buyer;

import com.netease.research.Vo.CartVo;
import com.netease.research.common.Const;
import com.netease.research.common.ServerResponse;
import com.netease.research.common.StatusCode;
import com.netease.research.pojo.Cart;
import com.netease.research.pojo.User;
import com.netease.research.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CartController {

    @Autowired
    private ICartService iCartService;

    @RequestMapping("add_cart.do")
    @ResponseBody
    public ServerResponse<Cart> add(HttpSession session, Integer productId, Integer quantity){
        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null || user.getRole()!=Const.Role.BUYER){
//       TODO     未登录 或者 不是顾客，跳转登录页面
            return ServerResponse.createByCode(StatusCode.NEED_LOGIN.getCode());
        }
        return iCartService.add(user.getId(),productId,quantity);
    }

    @RequestMapping("list_cart.do")
    @ResponseBody
    public ServerResponse<List<CartVo>> list(HttpSession session){
        User user=(User)session.getAttribute(Const.CURRENT_USER);
        if(user!=null) {
            return iCartService.list(user.getId());
        }
        return ServerResponse.createByCode(StatusCode.NEED_LOGIN.getCode());
    }
}
